package com.java2007.zhangyu.restaurant.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.java2007.zhangyu.restaurant.dao.AdminDao;
import com.java2007.zhangyu.restaurant.dao.impl.AdminDaoImpl;
import com.java2007.zhangyu.restaurant.pojo.Admin;
import com.java2007.zhangyu.restaurant.service.AdminService;
import com.java2007.zhangyu.restaurant.utils.MD5Utils;

import java.sql.SQLException;

/**
 * @author zy
 * @Version
 * @date 2021/01/28
 */
public class AdminServiceImpl implements AdminService {
    private AdminDao adminDao = new AdminDaoImpl();
    @Override
    public Admin login(String aName, String aPassword) throws Exception{
        try {
            //判断管理员账户名或密码是否为空
            if (StringUtils.isEmpty(aName) || StringUtils.isEmpty(aPassword)) {
                throw new RuntimeException("用户名或密码都不能为空");
            }

            Admin admin = adminDao.queryByAdminName(aName.trim());

            if(admin == null) {
                throw new RuntimeException("用户名或密码错误!!!");
            }

            //加密的密码  dbPwd
            String dbPwd = admin.getaPassword();

            //明文密码 apassword
            if(dbPwd.equals(MD5Utils.md5(aPassword))) {
                return admin;
            } else {
                throw new RuntimeException("用户名或密码错误!!!");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException("用户名或密码错误!!!");
        }
    }
}
